package com.erp.controller.customer;

import com.erp.common.core.controller.BaseController;
import com.erp.common.core.domain.AjaxResult;
import com.erp.common.core.page.TableDataInfo;
import com.erp.domain.ErpCustomer;

import com.erp.service.customer.ErpCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * --- 天道酬勤 ---
 *
 * @author ZhangYuqi
 * @date 2025/9/2
 * @desc
 */
@RestController
@RequestMapping("/erp/customer")
@Slf4j
public class ErpCustomerController extends BaseController {
    @Autowired
    private ErpCustomerService erpCustomerService;
    @GetMapping("/list")
    public TableDataInfo listCustomer(ErpCustomer erpCustomer) {

        startPage();
       List<ErpCustomer>list =  erpCustomerService.selectCustomerList(erpCustomer);

       return getDataTable(list);
    }

    //添加客户
    @PostMapping("/add")
    public AjaxResult addCustomer(@RequestBody ErpCustomer erpCustomer) {
        if (erpCustomer.getIsActive() == null) {
            erpCustomer.setIsActive(1); // 假设 1 表示启用，0 表示禁用
        }
        return toAjax(erpCustomerService.addCustomer(erpCustomer));
    }
    @GetMapping("/{customerId}")
    public AjaxResult getCustomer(@PathVariable("customerId") Long customerId) {
        ErpCustomer customer = erpCustomerService.selectCustomerById(customerId);
        return AjaxResult.success(customer);
    }
    @PutMapping("/update")
    public AjaxResult updateCustomer(@RequestBody ErpCustomer erpCustomer) {

        return toAjax(erpCustomerService.updateCustomer(erpCustomer));
    }
    @GetMapping("/view/{customerId}")
    public AjaxResult viewCustomer(@PathVariable("customerId") Long customerId) {
        ErpCustomer customer = erpCustomerService.selectCustomerById(customerId);
        return AjaxResult.success(customer);
    }
    @PutMapping("/enable/{customerId}")
    public AjaxResult enableCustomer(@PathVariable("customerId") Long customerId) {
        return toAjax(erpCustomerService.enableCustomer(customerId));
    }
    @PutMapping("/disable/{customerId}")
    public AjaxResult disableCustomer(@PathVariable("customerId") Long customerId) {
        return toAjax(erpCustomerService.disableCustomer(customerId));
    }
    // 检查客户是否关联其他表
    @GetMapping("/checkRelation/{customerId}")
    public AjaxResult checkCustomerRelation(@PathVariable("customerId") Long customerId) {
        return toAjax(erpCustomerService.checkCustomerRelation(customerId));
    }

}
